package drds.binlog;

import drds.binlog.event.FormatDescriptionLogEvent;
import drds.binlog.event.GtidLogEvent;
import drds.binlog.event.TableMapLogEvent;
import drds.binlog.parse.driver.packets.GtidSet;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO: Document Me!! NOTE: Log context will NOT write multi-threaded.
 */
public final class LogContext
{

    private final Map<Long, TableMapLogEvent> tableIdToTableMapLogEventMap = new HashMap<Long, TableMapLogEvent>();

    private FormatDescriptionLogEvent formatDescriptionLogEvent;

    private LogPosition logPosition;

    private GtidSet gtidSet;

    private GtidLogEvent gtidLogEvent; // save current gtid log event

    public LogContext()
    {
        this.formatDescriptionLogEvent = FormatDescriptionLogEvent.FORMAT_DESCRIPTION_EVENT_5_x;
    }

    public LogContext(FormatDescriptionLogEvent formatDescriptionLogEvent)
    {
        this.formatDescriptionLogEvent = formatDescriptionLogEvent;
    }

    public final LogPosition getLogPosition()
    {
        return logPosition;
    }

    public final void setLogPosition(LogPosition logPosition)
    {
        this.logPosition = logPosition;
    }

    public final FormatDescriptionLogEvent getFormatDescriptionLogEvent()
    {
        return formatDescriptionLogEvent;
    }

    public final void setFormatDescriptionLogEvent(FormatDescriptionLogEvent formatDescriptionLogEvent)
    {
        this.formatDescriptionLogEvent = formatDescriptionLogEvent;
    }

    public final void putTableMapLogEvent(TableMapLogEvent tableMapLogEvent)
    {
        tableIdToTableMapLogEventMap.put(Long.valueOf(tableMapLogEvent.getTableId()), tableMapLogEvent);
    }

    public final TableMapLogEvent getTableMapLogEvent(final long tableId)
    {
        return tableIdToTableMapLogEventMap.get(Long.valueOf(tableId));
    }

    public final void clearTableIdToTableMapLogEventMap()
    {
        tableIdToTableMapLogEventMap.clear();
    }

    public void reset()
    {
        formatDescriptionLogEvent = FormatDescriptionLogEvent.FORMAT_DESCRIPTION_EVENT_5_x;
        tableIdToTableMapLogEventMap.clear();
    }

    public GtidSet getGtidSet()
    {
        return gtidSet;
    }

    public void setGtidSet(GtidSet gtidSet)
    {
        this.gtidSet = gtidSet;
    }

    public GtidLogEvent getGtidLogEvent()
    {
        return gtidLogEvent;
    }

    public void setGtidLogEvent(GtidLogEvent gtidLogEvent)
    {
        this.gtidLogEvent = gtidLogEvent;
    }
}
